package com.atguigu.day17.map06;

import org.junit.Test;

import java.util.*;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/21 11:08
 * @注释 Map:
 *   特点: key 唯一
 *        key 相同 新的value 替换旧的value 喜新厌旧
 *   通用的方法:
 *            增 删 改 查
 *   遍历方式:
 *        1.先获取所有的key 再通过key 获取value
 *
 *        2.一次性获取所有的键值对
 */
public class MapTest {
    @Test
    public void test06(){
        Map<String,Integer> map = new HashMap<>();
        map.put("杨幂",6);
        map.put("赵丽颖",7);
        map.put("刘亦菲",8);
        map.put("刘诗诗",9);
        //一次性获取所有的键值对 存储到set内
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key+":"+value);
        }

    }


    @Test
    public void test05(){
        Map<String,Integer> map = new HashMap<>();
        map.put("杨幂",6);
        map.put("赵丽颖",7);
        map.put("刘亦菲",8);
        map.put("刘诗诗",9);
        //1.获取所有的key
        Set<String> keys = map.keySet();

        for (String key : keys) {
            //2.根据key 获取value
            Integer value = map.get(key);
            System.out.println(key+" "+value);
        }

    }



    @Test
    public void test04(){

        Map<String,Double> map = new HashMap<>();

        map.put("安琪拉",3.14);
        map.put("米莱迪",3.14);
        map.put("妲己",6.14);
        map.put("貂蝉",9.14);
        map.put("貂蝉",8.14);
        System.out.println("map.size() = " + map.size());
        System.out.println("map = " + map);

    }


    @Test
    public void test03() {
        Map<String, Integer> map = new HashMap<>();
        map.put("杨幂", 333);
        map.put("赵丽颖", 666);
        map.put("范冰冰", 999);
        map.put("刘亦菲", 555);
        System.out.println("map = " + map);
        System.out.println("map.size() = " + map.size());
        System.out.println("map.isEmpty() = " + map.isEmpty());
        map.clear();
        System.out.println("map.isEmpty() = " + map.isEmpty());

    }


    @Test
    public void test02() {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "张三");
        map.put(33, "李四");
        map.put(66, "王五");
        map.put(22, "陈一");

        System.out.println("map = " + map);//{1=张三, 33=李四, 66=王五, 22=陈一}
        //todo 获取所有的key
        Set<Integer> keys = map.keySet();
        for (Integer key : keys) {
            System.out.println("key = " + key);
        }
        //todo 获取所有的vlaue
        Collection<String> values = map.values();
        Iterator<String> iterator = values.iterator();
        while (iterator.hasNext()) {
            String value = iterator.next();
            System.out.println("value = " + value);
        }

    }


    @Test
    public void test01() {
        Map<String, String> map = new HashMap<>();
        //添加
        map.put("中国", "china");
        map.put("日本", "japan");
        map.put("俄罗斯", "russia");
        map.put("美国", "USA");

        System.out.println("map = " + map);//{美国=USA, 俄罗斯=russia, 中国=china, 日本=japan}

        //删除
        map.remove("日本");
        System.out.println("map = " + map);//{美国=USA, 俄罗斯=russia, 中国=china}
        //修改
        map.replace("中国", "CHINA");
        System.out.println("map = " + map);//{美国=USA, 俄罗斯=russia, 中国=CHINA}

        //查询
        String value = map.get("中国");
        System.out.println("value = " + value);//CHINA
        //判断指定的key 和value 是否在map内
        boolean containsKey = map.containsKey("中国");
        System.out.println("containsKey = " + containsKey);
        boolean containsValue = map.containsValue("USA2");
        System.out.println("containsValue = " + containsValue);


    }
}
